package com.scau.hwadee.examination.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.scau.hwadee.examination.entity.Course;
import com.scau.hwadee.examination.entity.Coursecorrespondingrelation;
import com.scau.hwadee.examination.entity.Nationwidecourse;
import com.scau.hwadee.examination.entity.Textbook;
import com.scau.hwadee.examination.mapper.CourseMapper;
import com.scau.hwadee.examination.mapper.CoursecorrespondingrelationMapper;
import com.scau.hwadee.examination.mapper.NationwidecourseMapper;
import com.scau.hwadee.examination.mapper.TextbookMapper;
import com.scau.hwadee.examination.service.CourseManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author YHX
 * @date 2019/7/30 20:51
 * description
 */
@Service
public class CourseManagerServiceImpl implements CourseManagerService {

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private TextbookMapper textbookMapper;

    @Autowired
    private NationwidecourseMapper nationwidecourseMapper;

    @Autowired
    private CoursecorrespondingrelationMapper coursecorrespondingrelationMapper;

    @Override
    public int insertCourseListByExcel(List<Course> courses) {
        try {
            return courseMapper.insertCourseListByExcel(courses);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int insertNationalCourseListByExcel(List<Nationwidecourse> nationwidecourses) {
        try {
            return nationwidecourseMapper.insertNationalCourseListByExcel(nationwidecourses);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public List<Course> getAllCourses2ExportExcel() {
        return courseMapper.selectAllCourses2ExportExcel();
    }

    @Override
    public int insertNewCourse(Course course) {
        try {
            return courseMapper.insert(course);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public PageInfo<Course> getAllCourses(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(courseMapper.selectAllCourse(pageNum, pageSize, null));
    }

    @Override
    public int updateCourseState(JSONArray courses) {
        List<Course> courseList = new ArrayList<>();
        for (int i = 0; i < courses.size(); i++) {
            Course course = new Course();
            JSONObject data = courses.getJSONObject(i);
            course.setCsId(data.get("courseId").toString());
            course.setCsState(data.get("courseState").toString());
            courseList.add(course);
        }
        try {
            return courseMapper.updateCourseState(courseList);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int updateCourseTextbook(String courseId, String textbookId) {
        try {
            Course course = new Course();
            course.setCsId(courseId);
            course.setTbIsbn(textbookId);
            int cnt = courseMapper.updateByPrimaryKeySelective(course);
            return cnt;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public Textbook getTextbookOfCourse(String courseId) {
        return textbookMapper.selectTextBookOfCourse(courseId);
    }

    @Override
    public int createNewNationalCourse(Nationwidecourse nationwidecourse) {
        try {
            return nationwidecourseMapper.insert(nationwidecourse);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int updateNationalCourse(Nationwidecourse nationwidecourse) {
        try {
            return nationwidecourseMapper.updateByPrimaryKey(nationwidecourse);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public PageInfo<Nationwidecourse> getAllNationalCourse(int pageNum, int pageSize) {
        return new PageInfo<>(nationwidecourseMapper.selectAllNationalCourse(pageNum, pageSize));
    }

    @Override
    public int deleteNationCourseList(JSONArray params) {
        try {
            List<Nationwidecourse> nationwidecourses = new ArrayList<>();
            for (int i = 0; i < params.size(); i++) {
                Nationwidecourse nationwidecourse = new Nationwidecourse();
                nationwidecourse.setNcId(params.getJSONObject(i).get("nationalCourseId").toString());
                nationwidecourses.add(nationwidecourse);
            }
            return nationwidecourseMapper.deleteListByPrimaryKey(nationwidecourses);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public PageInfo<Course> getAllPracticalOrTheoreticalCourse(int pageNum, int pageSize, String property,String mvId) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(courseMapper.selectAllPracticalOrTheoreticalCourse(pageNum, pageSize, property,mvId));
    }

    @Override
    public int insertCourseCorrespondingRelationList(List<Coursecorrespondingrelation> coursecorrespondingrelations) {
        try {
            return coursecorrespondingrelationMapper.insertList(coursecorrespondingrelations);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public Course getCourseByCsId(String csId) {
        try{
            return courseMapper.selectByPrimaryKey(csId) ;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}
